package com.taptech.doufu.util;


import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.widget.TextView;

import com.taptech.doufu.R;

import java.util.ArrayList;
import java.util.List;

import androidx.core.content.ContextCompat;

import static anet.channel.util.StringUtils.isNotBlank;

public class StringUtil {

	//
	public static void sprintf(StringBuffer buffer, String format, Object ...replace)
	{
		buffer.append(String.format(format, replace));
	}
	public static String  sprintfs( String originTitle,String time,String title ,String templateXml, String bodyXml,String refArticleContent,String sourArticleUrl)
	{
	 String xml=templateXml.replace("{{template_title}}", originTitle);
	 xml=xml.replace("{{template_time}}", time);
	 xml=xml.replace("{{template_from}}", title);
	 xml=xml.replace("{{Article_source}}", "");
//	xml=xml.replace("{{template_content}}", "<p><div class=\"content\"><p><div class='bigContainer' ><div id=\"bigImg0\" class='bigPhotoContainer'>"+
//	"<a><img class=\"displayed\" style=\"width:100%;\" src=\"http://imgold2.doufu.la/diaobaoimg/71/6B/ED169B7371F924E423FDE8457D3D.jpg\"/></a></div></div></p>");
	 if(bodyXml!=null){
		 xml=xml.replace("{{template_content}}",  bodyXml);
	 }
	 else{
		 xml=xml.replace("{{template_content}}",  "");
	 }
	 TTLog.d("xml", refArticleContent);
	 xml=xml.replace("{{template_refArticle}}", refArticleContent);
	 return xml;
	}
	public static boolean nullOrSpaceCheck(String content) {
		if (content == null) {
			return true;
		}
		
		content = content.trim();
		TTLog.e("nullOrSpaceCheck", content.length() + "");
		TTLog.e("isAllFullWidthSpace", isAllFullWidthSpace(content) + "");
		if (content.equals("") || isAllFullWidthSpace(content)) {
			return true;
		}
		
		return false;
	}
	
	public static boolean isAllFullWidthSpace(String content) {
		if (content == null) {
			return false;
		}
		
		int i = 0;
		for (; i < content.length(); i++) {
			if (content.charAt(i) != '　') {
				break;
			}
		}
		
		if (i == content.length()) {
			return true;
		}
		
		return false;
		
	}



	/**
	 * 过滤换行符、制表符、空格
	 * @param content
	 * @return
	 */
	public static String filterAllSpace(String content) {
		if (TextUtils.isEmpty(content)){
			return "";
		}
		content = content.replaceAll("\r\n", "");
		content = content.replaceAll("\n", "");
		content = content.replaceAll("\t", "");
		content = content.replaceAll("　", "");
		content = content.replaceAll(" ", "");
		return content.trim();
	}

	/**
	 * 过滤换行符
	 * @param content
	 * @return
	 */
	public static String filterLineFeed(String content) {
		if (TextUtils.isEmpty(content)){
			return "";
		}
		content = content.replaceAll("\r\n", "");
		content = content.replaceAll("\n", "");
		return content;
	}
	
	
	/**
	 * convert Url To File Name
	 * 
	 * */
	public static String convertUrlToSecretFileName(String url) {
        /*String[] strs = url.split("/");
        if (strs.length > 0) {
        	return strs[strs.length - 1] + ".img_cache";
        }*/
        
        if (url != null) {
        	url = url.replaceAll("(?i)http", "_");
        	url = url.replace(":", "_");
        	url = url.replace("/", "_");
        	url = url + ".img_cache";
        }
        
        TTLog.e("convertUrlToFileName", url);
        
        return url;
    }
	
	public static String convertUrlToFileName(String url) {
		if (url != null) {
			url = url.replaceAll("(?i)http", "_");
			url = url.replace(":", "_");
			url = url.replace("/", "_");
        }
        
        TTLog.e("convertUrlToFileName", url);
        
        return url+"df";
	}
	
	public static String subStringData(String data){
		String mData="";
		if(data==null||"".equals(data)||data.length()<11){
			return data;
		}
		mData=data.substring(2,10);
		return mData;
	}
	public static String subStringUrl(String url){
		String imageUrl="";
		if(url==null||"".equals(url) ||url.length()<21){
			return imageUrl;
		} 
		imageUrl=url.substring(21,url.length());
		return imageUrl;
	}

	public static boolean stringLength(String str){
		return str != null && str.length() >0;
	}


	public static String filterEmoji(String source,String slipStr) {
		try {
			if(isNotBlank(source)){
				return source.replaceAll("[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]", slipStr);
			}else{
				return source;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return source;
		}

	}
}
